package handler

import (
	"github.com/gin-gonic/gin"

	"torler.com/qingyue/apps/backend/internal/service"
	v1 "torler.com/qingyue/apps/backend/internal/transport/http/v1"
)

type BasicHandler struct {
	service *service.BasicService
}

func NewBasicHandler(service *service.BasicService) *BasicHandler {
	return &BasicHandler{
		service: service,
	}
}

// GetValidCode godoc
// @Summary      获取验证码

func (h *BasicHandler) GetValidCode(ctx *gin.Context) {
	req := new(v1.ValidCodeReq)
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.NewRsp(ctx).WithMsg(v1.ErrBadRequest).WithReason(err).Send()
		return
	}
	ret, err := h.service.SendValidCode(ctx, req)
	if err != nil {
		v1.NewRsp(ctx).WithMsg(v1.ErrValidCodeSendFailed).WithReason(err).Send()
		return
	}
	if ret {
		v1.NewRsp(ctx).WithMsg(v1.SucSendValidCode).Send()
	} else {
		v1.NewRsp(ctx).WithMsg(v1.ErrInternalServerError).Send()
		return
	}
}
